#include <QApplication>
#include <QPushButton>
#include <QFont>
#include <QWidget>
#include <QSizePolicy>
#include <QLCDNumber>
#include <QSlider>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QObject>
#include <QString>
#include <QColor>
#include <QListWidget>
#include <QPainter>
#include <QCheckBox>
#include <QLabel>
#include <QPalette>

#include <QStringList>
#include <QTreeWidget>
#include <QTreeWidgetItem>

#include <QFileDialog>

#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_marker.h>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <qwt_data.h>
#include <qwt_text.h>
#include <qwt_scale_engine.h>
#include <qwt_wheel.h>

#include <math.h>
#include <iostream>
#include <string>
#include <sstream>

#include "qplot.h"
#include "see.h"
#include "qmanager.h"
#include "globalconstants.h"
#include "errorwindow.h"


qManager::qManager(see* event,QWidget *parent):
    evento(0),
    viewDetBox(0),
    viewRefBox(0),
    viewDetWidget(0),
    viewRefWidget(0)
{
  evento = event;
}

qManager::~qManager()
{
  evento = 0;
}

void qManager::listTankChanged(int sId,eLT lt,int sIndex)
{
  //cout <<"\nqManager::listTankChanged(int sId)\n";
  emit detTankSelect(sId,lt);
  emit valueChanged(evento->getPmtTraces(sId));
  emit setInfo(&(evento->candList[sIndex]));
}

void qManager::detTankChanged(int sId)
{
  //cout <<"\nqManager::detTankChanged(int sId)\n";
  emit valueChanged(evento->getPmtTraces(sId));
  emit listTankSelect(sId);
  int ind=-1;
  for(int i=0;i< (evento->candList.size());i++){
    if(sId==evento->candList[i].sId){
      ind=i;
      break;
    }
  }
  emit setInfo(&(evento->candList[ind]));
}

void qManager::changeEvent(int eventIndexNo)
{
  evento->readEvent(eventIndexNo);
  emit newDetector(&(evento->statList));
  //cout << "\nqManager::changeEvent(int), emit newDetector(&(evento->statList))\n";
  emit setInfo(&(evento->candList[0]));
  emit setTankList(&(evento->candList));
  //cout << "\nqManager::changeEvent(int), emit setTankList(&(evento->candList))\n";
  QApplication::restoreOverrideCursor();
}

void qManager::changeEvent(see* newEvent)
{
  evento = newEvent;
  emit newDetector(&(evento->statList));
  emit setInfo(&(evento->candList[0]));
  emit setTankList(&(evento->candList));
}

void qManager::setTankListPointer(qTankList* tankList)
{
  tankListPointer=tankList;
}

void qManager::setViewDetBox(QCheckBox* detBox)
{
  viewDetBox=detBox;
  QObject::connect(viewDetBox, SIGNAL(stateChanged(int)), this, SLOT(viewDetChanged(int)));
}

void qManager::setViewRefBox(QCheckBox* refBox)
{
  viewRefBox=refBox;
  QObject::connect(viewRefBox, SIGNAL(stateChanged(int)), this, SLOT(viewRefChanged(int)));
}

void qManager::setViewDetWidget(QWidget* detWidget)
{
  viewDetWidget=detWidget;
  QObject::connect(viewDetWidget, SIGNAL(closedWindow()), this, SLOT(detWindClosed()));
}

void qManager::setViewRefWidget(QWidget* refWidget)
{
  viewRefWidget=refWidget;
  QObject::connect(viewRefWidget, SIGNAL(closedWindow()), this, SLOT(refWindClosed()));
}

void qManager::viewDetChanged(int value)
{
  if(value==0) viewDetWidget->hide();
  if(value==2) viewDetWidget->show();
}

void qManager::viewRefChanged(int value)
{
  if(value==0) viewRefWidget->hide();
  if(value==2) viewRefWidget->show();
}

void qManager::viewInfChanged(int value)
{
  if(value==0) viewRefWidget->hide();
  if(value==2) viewRefWidget->show();
}

void qManager::detWindClosed()
{
  viewDetBox->setCheckState(Qt::Unchecked);
}

void qManager::refWindClosed()
{
  viewRefBox->setCheckState(Qt::Unchecked);
}

void qManager::infWindClosed()
{
  viewRefBox->setCheckState(Qt::Unchecked);
}

void qManager::openFile()
{
  bool isOK=0;
  do{
    QApplication::restoreOverrideCursor();
    QString s = QFileDialog::getOpenFileName(this,"Seleccione un archivo","","*.root");
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    if(s.toStdString()==""){
      QApplication::restoreOverrideCursor();
      return;
    }
    isOK=evento->openFile(const_cast<char*>(s.toStdString().c_str()));
    errorWindow errorW;
    if(!isOK){
      QApplication::restoreOverrideCursor();
      errorW.exec();
    }
  }while(!isOK);
  
  evento->readEvent(evento->eventsList[0].fileIndex);
  QApplication::restoreOverrideCursor();
  emit newDetector(&(evento->statList));
  emit setEventsList(&(evento->eventsList));
  emit setTankList(&(evento->candList));
  return;
}
